package com.ruoyi.system.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.webservice.SoapClient;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.json.XML;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.domain.HUAWEI.BarcodeRelationSendDTO;
import com.ruoyi.system.domain.HUAWEI.EmsBarcodeRelation;
import com.ruoyi.system.mapper.HUAWEI.EmsBarcodeRelationMapper;
import com.ruoyi.system.service.IEmsBarcodeRelationService;
import com.ruoyi.system.utils.InterfaceLogUtil.InterfaceLogUtil;
import com.ruoyi.system.utils.huaweiInterfaceUtil.HuaweiInterfaceHttps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author GE-LV5
 * @create 2023/4/10 15:10
 */
@Service
@Slf4j
public class EmsBarcodeRelationServiceImpl extends ServiceImpl<EmsBarcodeRelationMapper, EmsBarcodeRelation> implements IEmsBarcodeRelationService {

    @Autowired
    private InterfaceLogUtil interfaceLogUtil;

    @Override
    public void unbindingRelation(List<EmsBarcodeRelation> list,Date updatedDate) {
        this.sendHuaweitmzs(list,updatedDate);
    }

    @Async("asyncExecutor")
    public void sendHuaweitmzs(List<EmsBarcodeRelation> list,Date updatedDate) {
        Date startDate = new Date();
        try{
            list.stream().forEach(i->{
                i.setSegment5(1);
                String xml = "<![CDATA[<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                        "<BarcodeRelationSend>\n" +
                        "<Import>\n" +
                        "<PO>" + i.getPo() + "</PO>\n" +
                        "<VendorID>22827731</VendorID>\n" +
                        "<ORGID>157</ORGID>\n" +
                        "<ItemCode>" + i.getItemCode() + "</ItemCode>\n" +
                        "<Barcode>" + i.getBarCode() + "</Barcode>\n" +
                        "<SonBarcode>" + i.getSonBarCode() + "</SonBarcode>\n" +
                        "<SonItemCode>" + i.getSonItemCode() + "</SonItemCode>\n" +
                        "<Quantity>1</Quantity>\n" +
                        "<Supplier>" + i.getSupplier() + "</Supplier>\n" +
                        "<Manufactory></Manufactory>\n" +
                        "<UploadFlag>0</UploadFlag>\n" +
                        "<CheckMessage></CheckMessage>\n" +
                        "<CreatedBy>LD001</CreatedBy>\n" +
                        "<CreatedDate>" + DateUtil.format(i.getCreatedDate(),"yyyy-MM-dd HH:mm:ss") + "</CreatedDate>\n" +
                        "<UpdatedBy></UpdatedBy>\n" +
                        "<UpdatedDate>"+DateUtil.format(updatedDate,"yyyy-MM-dd HH:mm:ss")+"</UpdatedDate>\n" +
                        "<Segment1>N</Segment1>\n" +
                        "<Segment2>N</Segment2>\n" +
                        "<Segment3>" + i.getSegment3() + "</Segment3>\n" +
                        "<Segment4></Segment4>\n" +
                        "<Segment5>"+i.getSegment5()+"</Segment5>\n" +
                        "<Segment6></Segment6>\n" +
                        "<Segment7></Segment7>\n" +
                        "<ItemID></ItemID>\n" +
                        "<SonItemID></SonItemID>\n" +
                        "<CheckFlag>0</CheckFlag>\n" +
                        "<EmsTransID>" + i.getEmsTransId() + "</EmsTransID>\n" +
                        "<CheckInfo></CheckInfo>\n" +
                        "<Segment8></Segment8>\n" +
                        "<Segment9></Segment9>\n" +
                        "<Segment10>" + i.getSegment10() + "</Segment10>\n" +
                        "<Segment11>" + i.getSegment11() + "</Segment11>\n" +
                        "<Segment12></Segment12>\n" +
                        "<Segment13></Segment13>\n" +
                        "<Segment14>40033</Segment14>\n" +
                        "<Segment15>EMS</Segment15>\n" +
                        "</Import>\n" +
                        "</BarcodeRelationSend>]]>";

                //改为https方式传输数据20250415
//            String date = DateUtil.format(new Date(), "yyyyMMdd'T'HHmmss'Z'");
//            BarcodeRelationSendDTO barcodeRelationSendDTO = new BarcodeRelationSendDTO(i);
//            String res = HuaweiInterfaceHttps.sendHuawei(HuaweiInterfaceHttps.serviceUriBarcodeRelationSend,HuaweiInterfaceHttps.http_medhod_BarcodeRelationSend,
//                            JSONUtil.toJsonStr(barcodeRelationSendDTO),date);

                SoapClient client = SoapClient.create("http://cmes.huawei.com/hw_manufacture/MidLayer.asmx")
                        .setMethod("Get_Info_Frmbarcode", "http://Auto.huawei.com.cn/")
                        .setParam("sTaskType", "BarcodeRelationSend")
                        .setParam("sImport", xml);

                // 发送请求，参数true表示返回一个格式化后的XML内容
                // 返回内容为XML字符串，可以配合XmlUtil解析这个响应
                String send = client.send(true);
                JSONObject object = XML.toJSONObject(send);
                Map map3 = JSONUtil.toBean(JSONUtil.toJsonStr(object), Map.class);
                String res = JSONUtil.toJsonStr(map3);
                i.setUpdatedDate(updatedDate);
                i.setResUntie(res);
                if(!res.contains("Success")){
                    i.setSegment5(null);//解绑状态
                }
                baseMapper.update(i,new QueryWrapper<EmsBarcodeRelation>().eq("ems_trans_id",i.getEmsTransId()));
            });
        }catch (Exception e){
            //解绑失败日志记录
            interfaceLogUtil.insertLog(JSONUtil.toJsonStr(list),
                    startDate, new Date(), InterfaceLogUtil.DIR_MES_MOM,
                    InterfaceLogUtil.TYPE_HTTP,
                    e.getMessage(),
                    InterfaceLogUtil.INTERFACENAME_JBYC
            );
        }
    }

}
